<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-button type="primary" plain icon="Plus" @click="handleEditAll"
    >批量修改</el-button
  >
  <el-button type="success" plain icon="Check" @click="handleCopy"
    >复制上月</el-button
  >
  <el-button color="#626aef" :dark="isDark" plain icon="Download" @click="handleUP"
    >导入</el-button
  >
  <el-table
    :data="KpiRuleList"
    @selection-change="selectChange"
    border
    style="
      width: 100%;
      margin: 5px 0px;
      border-radius: 8px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :cell-style="{ textAlign: 'center' }"
  >
    <el-table-column type="selection" width="55" :selection="selection" />
    <el-table-column
      label="员工姓名"
      prop="empName"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column
      label="服务地区"
      prop="region"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column
      label="地区系数"
      prop="regionCoefficient"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column
      label="岗位系数"
      prop="positionCoefficient"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column
      label="基础工资/￥"
      prop="wage"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column
      label="绩效工资/￥"
      prop="performanceWage"
      width="110"
      class-name="my-content"
    ></el-table-column>
    <el-table-column prop="state" label="状态" width="70">
      <template #default="scope">
        <el-tag
          :type="scope.row.tag === 'Home' ? '' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column label="最后更新时间" width="200">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="KpiListPage.total > 0"
    :total="KpiListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="KpiListPage.size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialogFormVisible"
    :CadreList="CadreList"
    :close="close"
    :update="update"
    :form="form"
  ></Dialog>
  <AllDialog
    v-model="dialog"
    :close="closeAll"
    :update="update"
    :selection="selection"
  >
  </AllDialog>
  <UploadExcel
    v-model="upload.open"
    :title="upload.upLoadTitle"
    :url="importUrl"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate"
  ></UploadExcel>
  <FormDialog
    v-model="formDialog"
    :close="closeForm"
    :update="update"
  ></FormDialog>
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import {
  getCoefficientPage,
  copy,
  getCoefficientSearch,
} from "@/api/coefficient";
import Dialog from "./coefficientDialog";
import FormDialog from "./coeForm";
import AllDialog from "./coeAllChangeDialog";
import { ElMessage } from "element-plus";
import UploadExcel from "@/components/SendAndDownload/index";
import {getCoefficientTemplate} from "@/api/template";

const searchForm = ref({
  empName: "",
  page: 1,
  page_size: 10,
});
const KpiListPage = ref([]);
const KpiRuleList = ref([]);
onBeforeMount(() => {
  getCoefficientPage(searchForm.value).then((data) => {
    KpiListPage.value = data.data;
    KpiRuleList.value = data.data.records;
  });
});
watch(
  KpiRuleList,
  (newVal) => {
    console.log("list发生了改变");
  },
  { deep: true }
);

const selection = ref([]);
const selectChange = (select) => {
  selection.value = select;
};
const dialog = ref(false);
const handleEditAll = (index, row) => {
  dialog.value = true;
};
const closeAll = () => {
  dialog.value = false;
};

const formDialog = ref(false);
const handleCopy = () => {
  formDialog.value = true;
};
const closeForm = () => {
  formDialog.value = false;
};

const importUrl = ref("/EmpCoefficient/upload");
const upload = ref({
  open: false,
  upLoadTitle: "员工绩效系数上传",
});
const onSuccess = (res) => {
  getCoefficientPage(searchForm.value).then((data) => {
    KpiListPage.value = data.data;
    KpiRuleList.value = data.data.records;
  });
  upload.value.open = false;
};
const handleUP = () => {
  upload.value.open = true;
};
const onDownTemplate = () => {
  getCoefficientTemplate()
};

const form = ref([]);
const dialogFormVisible = ref(false);
const handleEdit = (index, row) => {
  form.value = row;
  dialogFormVisible.value = true;
};

const handlePagination = (value) => {
  if (searchForm.value.empName == "") {
    getCoefficientPage(value).then((data) => {
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    });
  } else {
    searchForm.value.page = value.page;
    searchForm.value.page_size = value.page_size;
    getCoefficientSearch(searchForm.value).then((data) => {
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    });
  }
};

const searchEvent = () => {
  getCoefficientSearch(searchForm.value).then((data) => {
    KpiListPage.value = data.data;
    KpiRuleList.value = data.data.records;
  });
};

const close = () => {
  dialogFormVisible.value = false;
};
const update = (form) => {
  searchForm.empName = "";
  getCoefficientPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};
</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 350px;
  height: 47px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 47px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>